import http.client
import json
import time
from datetime import datetime

rapidapi_key = "1aec7c96abmsh49ebfef3825b107p10c475jsn3624af404e84"


def bin_checker(bin_number):
    """
    根据 bin 查询当前可能存在的银行，并返回对应的银行信息
    :param bin_number bin代码
    :returns
    """
    server_url = "bin-ip-checker.p.rapidapi.com"

    content_type = "application/json"

    conn = http.client.HTTPSConnection(server_url)
    payload = f"{{\"bin\":\"{bin_number}\"}}"
    headers = {
        'x-rapidapi-key': rapidapi_key,
        'x-rapidapi-host': server_url,
        'Content-Type': content_type
    }
    conn.request("POST", f"/?bin={bin_number}", payload, headers)
    res = conn.getresponse()
    data = res.read().decode("utf-8")
    if "The BIN number doesn't exist!" in data:
        return None
    return data


def json_analysis_BIN(BIN):
    datas = bin_checker(bin_number=BIN)
    if datas is not None:
        data = json.loads(datas)
        if data.get("success"):
            bins = data.get("BIN")
            number = bins.get("number")
            brand = bins.get("brand")
            scheme = bins.get("scheme")
            type = bins.get("type")
            level = bins.get("level")
            is_commercial = bins.get("is_commercial")
            is_prepaid = bins.get("is_prepaid")
            currency = bins.get("currency")
            issuer = bins.get("issuer")
            country = bins.get("country")
            if issuer is not None:
                issuer_name = issuer.get("name")
                issuer_website = issuer.get("website")
                issuer_phone = issuer.get("phone")
            if country is not None:
                country_name = country.get("name")
                country_native = country.get("native")
                country_flag = country.get("flag")
                country_numeric = country.get("numeric")
                country_capital = country.get("capital")
                country_currency = country.get("currency")
                country_currency_name = country.get("currency_name")
                country_currency_symbol = country.get("currency_symbol")
                country_region = country.get("region")
                country_subregion = country.get("subregion")
                country_idd = country.get("idd")
                country_alpha2 = country.get("alpha2")
                country_alpha3 = country.get("alpha3")
                country_language = country.get("language")
                country_language_code = country.get("language_code")
            item = {
                "number": number,
                "brand": brand,
                "scheme": scheme,
                "type": type,
                "level": level,
                "is_commercial": is_commercial,
                "is_prepaid": is_prepaid,
                "currency": currency,
                "issuer_name": issuer_name,
                "issuer_website": issuer_website,
                "issuer_phone": issuer_phone,
                "country_name": country_name,
                "country_native": country_native,
                "country_flag": country_flag,
                "country_numeric": country_numeric,
                "country_capital": country_capital,
                "country_currency": country_currency,
                "country_currency_name": country_currency_name,
                "country_currency_symbol": country_currency_symbol,
                "country_region": country_region,
                "country_subregion": country_subregion,
                "country_idd": country_idd,
                "country_alpha2": country_alpha2,
                "country_alpha3": country_alpha3,
                "country_language": country_language,
                "country_language_code": country_language_code,
                "create_time": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                "update_time": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
            }
            return item
    return None


if __name__ == '__main__':
    bin_value = 441590
    start_time = time.time()
    result = bin_checker(bin_number=bin_value)
    end_time = time.time()
    request_time = end_time - start_time
    print(request_time)
    print(result)
